package com.cb.spzx.manager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.spzx.manager.service.ProductSpecService;
import com.cb.spzx.model.entity.product.ProductSpec;
import com.cb.spzx.model.vo.common.Result;
import com.cb.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Tag(name = "商品规格管理")
@RestController
@RequestMapping(value="/admin/product/productSpec")
public class ProductSpecController {

    @Autowired
    private ProductSpecService productSpecService;

    @Operation(summary = "分页查询商品规格")
    @GetMapping("/{page}/{limit}")
    public Result findByPage(@PathVariable Integer page, @PathVariable Integer limit){
        Page<ProductSpec> productSpecPage = new Page<>(page, limit);
        productSpecService.findByPage(productSpecPage);
        return Result.build(productSpecPage, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加商品规格")
    @PostMapping("save")
    public Result saveProductSpec(@RequestBody ProductSpec productSpec){
        productSpecService.save(productSpec);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改商品规格")
    @PutMapping("updateById")
    public Result updateById(@RequestBody ProductSpec productSpec){
        productSpec.setUpdateTime(new Date());
        productSpecService.updateById(productSpec);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除商品规格")
    @DeleteMapping("/deleteById/{id}")
    public Result removeById(@PathVariable Long id){
        productSpecService.removeById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
